package com.ejuornal.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.ejuornal.bean.Page;
import com.ejuornal.bean.PerPeriodical;
import com.ejuornal.bean.PerPeriodicalSub;
import com.ejuornal.dao.PerPeriodicalDao;
import com.ejuornal.service.PerPeriodicalService;

@Component("perPeriodicalService")
public class PerPeriodicalServiceImpl implements PerPeriodicalService {
	private PerPeriodicalDao perPeriodicalDao;

	public void add(PerPeriodical perPeriodical) {
		String sql = "INSERT INTO t_per_periodical VALUES(?, ?, ?, ?, ?, ?, ?, ?)";
		perPeriodicalDao.add(
				sql,
				new Object[] { 
						null, 
						perPeriodical.getNumber(),
						perPeriodical.getIntroduce(),
						perPeriodical.getKeywords(), 
						perPeriodical.getStatus(),
						perPeriodical.getPeriodicalId(), 
						null,
						null});
	}
	
	public int add_2(final  PerPeriodical perPeriodical){
		return perPeriodicalDao.add(perPeriodical);
	}
	
	@Override
	public void update(PerPeriodical perPeriodical) {
		String sql = "UPDATE t_per_periodical SET " + 
						"status=?, introduce=?, keywords=?, checkResult=? WHERE perPeriodicalId = ?";
		perPeriodicalDao.update(sql, new Object[] { perPeriodical.getStatus(),
													perPeriodical.getIntroduce(), 
													perPeriodical.getKeywords(),
													perPeriodical.getCheckResult(),
													perPeriodical.getPerPeriodicalId(),
													});
	}
	@Override
	public void updateStatus(PerPeriodical perPeriodical, String status) {
		String sql = "UPDATE t_per_periodical SET status = ?, checkResult = ? WHERE perPeriodicalId = ?";
		perPeriodicalDao.update(sql, new Object[] { status, perPeriodical.getCheckResult(), perPeriodical.getPerPeriodicalId() });
	}
	@Override
	public void updateScore(int perPeriodicalId, float score) {
		String sql = "UPDATE t_per_periodical SET score = ? WHERE perPeriodicalId = ?";
		perPeriodicalDao.update(sql, new Object[] { score, perPeriodicalId });
	}

	public void delete(int... perPeriodicalId) {
		String sql = "DELETE FROM t_per_periodical WHERE perPeriodicalId = ?";
		perPeriodicalDao.batchDelete(sql, perPeriodicalId);
	}

	public PerPeriodical findById(int perPeriodicalId) {

		return perPeriodicalDao.findById(perPeriodicalId);
	}

	@Override
	public PerPeriodical findByPeriodicalNum(int periodicalId, int number) {
		return perPeriodicalDao.findUpNextPerPeriodical(periodicalId, number);
	}

	public PerPeriodical findUpPerPeriodical(int periodicalId, int number) {
		return perPeriodicalDao.findUpNextPerPeriodical(periodicalId, number-1);
	}

	public PerPeriodical findNextPerPeriodical(int periodicalId, int number) {
		return perPeriodicalDao.findUpNextPerPeriodical(periodicalId, number+1);
	}

	public Page<PerPeriodical> findByStatus(int current_record,String url, String status) {
		String hsql = "WHERE status = ?";
		
		return perPeriodicalDao.find(current_record, url, hsql, new Object[]{ status });
	}

	public Page<PerPeriodical> findByPeriodicalId(int current_record, String url, int periodicalId) {
		String hsql = "WHERE periodicalId = ?";
		
		return perPeriodicalDao.find(current_record, url, hsql, new Object[]{ periodicalId });
	}

	public Page<PerPeriodical> findByOrder(int current_record, String url, String property, String order) {
		String hsql = "ORDER BY ? ?";
		
		return perPeriodicalDao.find(current_record, url, hsql, new Object[]{ property, order });
	}

	public List<PerPeriodical> findNew(int count) {
		return perPeriodicalDao.findNew(count);
	}
	
	public List<PerPeriodical> findPerPeriodicalNumbers(int periodicalId, String status) {
		return perPeriodicalDao.findPerPeriodicalNumbers(periodicalId, status);
	}
	
	public List<PerPeriodical> findPerPerEditor(int userId, String status) {
		return perPeriodicalDao.findPerPerEditor(userId, status);
	}

	public List<PerPeriodical> findTop(int count) {
		return perPeriodicalDao.findTop(count);
	}
	
	@Override
	public PerPeriodical findNewPerPeriodical(int periodicalId, String status) {
		return perPeriodicalDao.findNewPerPeriodical(periodicalId, status);
	}

	@Override
	public Page<PerPeriodical> findByUserIdPPStatus(int current_record, String url, int userId, String status) {
		String sql_1 = "SELECT COUNT(*) from t_periodical p INNER JOIN t_per_periodical pp ON p.periodicalId = pp.periodicalId INNER JOIN t_category c ON p.categoryId = c.categoryId WHERE p.userId=? AND pp.status=?";
		String sql_2 = "SELECT p.*,pp.perPeriodicalId,pp.number,c.name AS cname from t_periodical p INNER JOIN t_per_periodical pp ON p.periodicalId = pp.periodicalId INNER JOIN t_category c ON p.categoryId = c.categoryId WHERE p.userId=? AND pp.status=? LIMIT " + current_record + ", 10";
		return perPeriodicalDao.find(current_record, url, sql_1, sql_2, new Object[]{ userId, status });
//		return perPeriodicalDao.find(current_record, url, hsql, new Object[]{ userId, status });
	}
	@Override
	public Page<PerPeriodical> findByPeriodicalIdPPStatus(int current_record, String url, int periodicalId, String status) {
		String sql_1 = "SELECT COUNT(*) from t_periodical p INNER JOIN t_per_periodical pp ON p.periodicalId = pp.periodicalId INNER JOIN t_category c ON p.categoryId = c.categoryId WHERE p.periodicalId=? AND pp.status=?";
		String sql_2 = "SELECT p.*,pp.perPeriodicalId,pp.number,c.name AS cname from t_periodical p INNER JOIN t_per_periodical pp ON p.periodicalId = pp.periodicalId INNER JOIN t_category c ON p.categoryId = c.categoryId WHERE p.periodicalId=? AND pp.status=? LIMIT " + current_record + ", 10";
		return perPeriodicalDao.find(current_record, url, sql_1, sql_2, new Object[]{ periodicalId, status });
//		String hsql = "WHERE pperiodicalId=? AND ppstatus=?";
//		return perPeriodicalDao.find(current_record, url, hsql, new Object[]{ periodicalId, status });
	}
	
	public PerPeriodicalDao getPerPeriodicalDao() {
		return perPeriodicalDao;
	}

	@Resource(name = "perPeriodicalDao")
	public void setPerPeriodicalDao(PerPeriodicalDao perPeriodicalDao) {
		this.perPeriodicalDao = perPeriodicalDao;
	}

	@Override
	public List<PerPeriodicalSub> findBystatus(int status) {
		return perPeriodicalDao.findBystatus(status);
	}

	@Override
	public void add(PerPeriodicalSub pps) {
		perPeriodicalDao.add(pps);
	}

	@Override
	public void update(PerPeriodicalSub pps) {
		
	}
}
